import { cloneDeep } from 'lodash';
import { toRefs } from 'vue';
import useComponentConfig from '@/views/design-center/store/component-config';
const useComponentConfigStore = useComponentConfig();

const { pageConfig } = toRefs(useComponentConfigStore);

// 通过id,查找数组下的list对应的id,并修改
export const getIdAndEditIt = (list, id, objKey, obj) => {
    // console.log('--------formData', objKey, obj);
    for (const item of list) {
        // console.log(item.id, id, 111111111111, obj);
        if (item.id === id) {
            obj &&
                Object.entries(obj).forEach(([key, value]) => {
                    if (objKey && item.type !== 'group') {
                        // setups和styles层级下的数据修改
                        // console.log('来了吗88888', item, objKey, key, value);
                        item[objKey][key] = value;
                    } else {
                        item[key] = value;
                        // item.type层级的修改
                        console.log('来了吗999999999', key, item, value, item[key]);
                    }

                    // console.log('object--------', objKey, key, item);
                });

            return false;
        }
        if (item.children) {
            getIdAndEditIt(item.children, id, objKey, obj);
        }
    }
};

/**
 * @description: 递归删除数组中指定的id
 * @param {*} list
 * @param {*} id
 * @return {*}
 */
export const getIdAndDeleteIt = (list, id) => {
    if (!list) return [];
    return list.filter((item) => {
        if (item.id !== id && item.children?.length) {
            item.children = getIdAndDeleteIt(item.children, id);
        }
        return item.id !== id;
    });
};

/**
 * @description:树状组合结构扁平化处理
 * @param {*} list
 * @return {*}
 */
export const flatList = (list) => {
    let copyList = [];
    for (const item of list) {
        if (item.type !== 'group') {
            copyList.push(item);
        }
        if (item.children) {
            copyList.push(flatList(item.children));
        }
    }
    return copyList.flat();
};

//用于复制后计数的对象
let countNum = {};
/**
 * @description: 使组合和组合下的元素,id唯一
 * @param {*} list
 * @return {*}
 */
const makeIdUniqueChild = (list) => {
    list.forEach((element, index) => {
        if (!element.children) {
            countNum[element.setups?.componentName] = countNum[element.setups?.componentName] ? ++countNum[element.setups?.componentName] : 1;

            countNum[element.type] = countNum[element.type] ? ++countNum[element.type] : 1;

            element.id = `key${Date.now() + index + 1 + 10}`;

            element.setups.componentName = `${element.setups.componentName}副本 ${countNum[element.setups.componentName]}`;

            element.styles = {
                ...element.styles,
                x: element.styles?.x + countNum[element.type] * 8,
                y: element.styles?.y + countNum[element.type] * 8
            };
        } else {
            // 分组以label进行累加

            countNum[element.label] = countNum[element.label] ? ++countNum[element.label] : 1;

            element.id = `key${Date.now() + index + 1}`;

            element.label = `${element.label}副本 ${countNum[element.label]}`;

            makeIdUniqueChild(element.children);
        }
    });
    return list;
};

/**
 * @description: 使组合和组合下的元素,id唯一.类型计数累加
 * @param {*} list
 * @return {*}
 */
export const makeIdUnique = (obj) => {
    if (obj.children?.length > 0) {
        countNum[obj.label] = countNum[obj.label] ? ++countNum[obj.label] : 1; //复制的分组以原有的label进行累加

        obj.id = `key${Date.now()}`;

        obj.label = `${obj.label}副本 ${countNum[obj.label]}`;

        obj.children = makeIdUniqueChild(obj.children);
    } else {
        countNum[obj.setups.componentName] = countNum[obj.setups.componentName] ? ++countNum[obj.setups.componentName] : 1; //复制的元素名称已原有名称累加

        countNum[obj.type] = countNum[obj.type] ? ++countNum[obj.type] : 1; //复制的元素位置以组件类型进行累加

        obj.id = `key${Date.now() + 1}`;

        obj.setups.componentName = `${obj.setups.componentName}副本 ${countNum[obj.setups.componentName]}`;

        obj.styles = {
            ...obj.styles,
            x: obj.styles?.x + countNum[obj.type] * 8,
            y: obj.styles?.y + countNum[obj.type] * 8
        };
    }
    return obj;
};
//获取所有父级id
export const searchParent = (list, key) => {
    let t = [];
    for (let i = 0; i < list.length; i++) {
      const e = list[i];
      if (e.id === key) {
        //若查询到对应的节点，则直接返回
        t.push(e.id);
        break;
      } else if (e.children && e.children.length !== 0) {
        //判断是否还有子节点，若有对子节点进行循环调用
        let p = searchParent(e.children, key);
        //若p的长度不为0，则说明子节点在该节点的children内，记录此节点，并终止循环
        if (p.length !== 0) {
          p.unshift(e.id);
          t = p;
          break;
        }
      }
    }
    return t;
  };
